﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using FrienDev.Data;
using FrienDev.BusinessRules;
using System.IO;

public partial class Controls_Activity_ImageListDetail : System.Web.UI.UserControl
{
    private int m_activityId;
    private int m_PageSize;
    private int m_Count;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["activityid"]))
        {
            m_activityId = Convert.ToInt32(Request.QueryString["activityid"]);
            //hl_AddImage.Attributes.Add("onmouseover", "javascript:tip_over();");
            //hl_AddImage.Attributes.Add("onmouseout", "javascript:tip_overend();");
            //hl_AddImage.Attributes.Add("onmousedown", "javascript:tip_overend();");
            hl_AddImage.NavigateUrl = "~/Activity/ImageUpload.aspx?activityid=" + m_activityId;
            Pager.PageChanged += new EventHandler(Pager_PageChanged);
            Pager.NumericButtonTextFormatString = "<span style='font-size:14px;'>{0}</span>";
            dl_ActivityImage.ItemDataBound += new DataListItemEventHandler(dl_ActivityImage_ItemDataBound);
            dl_ActivityImage.ItemCommand += new DataListCommandEventHandler(dl_ActivityImage_ItemCommand);
            InitDL_ActivityImage();
        }
    }

    void Pager_PageChanged(object sender, EventArgs e)
    {
        InitdlActivityImage();
    }

    void dl_ActivityImage_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "DeleteImage")
        {
            int itemIndex = ((DataListItem)((LinkButton)e.CommandSource).NamingContainer).ItemIndex;
            Label lbImageID = (Label)dl_ActivityImage.Items[itemIndex].FindControl("lb_ImageID");
            PhotoRules.Instance.DeleteActivityPhoto(m_activityId, Convert.ToInt32(lbImageID.Text));
            string bpath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\back\\");
            string spath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\s\\");
            string mpath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\m\\");
            Label lbFileName = (Label)dl_ActivityImage.Items[itemIndex].FindControl("lb_FileName");
            if (File.Exists(bpath + lbFileName.Text))
            {
                File.Delete(bpath + lbFileName.Text);
            }
            if (File.Exists(spath + lbFileName.Text))
            {
                File.Delete(spath + lbFileName.Text);
            }
            if (File.Exists(mpath + lbFileName.Text))
            {
                File.Delete(mpath + lbFileName.Text);
            }
            InitDL_ActivityImage();
            InitdlActivityImage();
            up_ActivityImage.Update();
        }
        if (e.CommandName == "EditImage")
        {
            int itemIndex = ((DataListItem)((LinkButton)e.CommandSource).NamingContainer).ItemIndex;
            Label lbImageID = (Label)dl_ActivityImage.Items[itemIndex].FindControl("lb_ImageID");
            Response.Redirect("~/Activity/ImageEdit.aspx?activityid=" + m_activityId + "&imageid=" + lbImageID.Text);
        }
    }

    void dl_ActivityImage_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Image img = (Image)e.Item.FindControl("img_ActivityImage");
        img.ImageUrl = PhotoRules.Instance.GetActivitySmallPhotoUrl(m_activityId, img.ImageUrl);
        Label lbImageID = (Label)e.Item.FindControl("lb_ImageID");
        HyperLink hlActivityImage = (HyperLink)e.Item.FindControl("hl_ActivityImage");
        hlActivityImage.NavigateUrl = "~/Activity/ImageDetail.aspx?activityid=" + m_activityId + "&imageid=" + lbImageID.Text; ;
        HyperLink hlActivityImageDescription = (HyperLink)e.Item.FindControl("hl_ActivityImageDescription");
        hlActivityImageDescription.NavigateUrl = "~/Activity/ImageDetail.aspx?activityid=" + m_activityId + "&imageid=" + lbImageID.Text;
        if (hlActivityImageDescription.Text.Trim() == "")
        {
            hlActivityImageDescription.Visible = false;
        }
        else
        {
            hlActivityImageDescription.ToolTip = hlActivityImageDescription.Text;
            hlActivityImageDescription.Text = HtmlHelper.InterceptSubString(hlActivityImageDescription.Text, 22, false);
        }
        HyperLink hlUser = (HyperLink)e.Item.FindControl("hl_User");
        hlUser.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(hlUser.Text);
        hlUser.Text = UserRules.Instance.GetUserByUserId(hlUser.Text).FullName;
        Label lbUserId = (Label)e.Item.FindControl("lb_UserId");
        Panel pnEdit = (Panel)e.Item.FindControl("pn_Edit");
        LinkButton lbDelete = (LinkButton)e.Item.FindControl("lb_Delete");
        if (ActivityRules.Instance.IsActivityPromoter(m_activityId, UserRules.Instance.GetCurrentUserId()) || lbUserId.Text == UserRules.Instance.GetCurrentUserId())
        {
            lbDelete.OnClientClick = "return confirm('确定要删除这张照片么？')";
            pnEdit.Visible = true;
        }
    }

    private void InitDL_ActivityImage()
    {
        m_Count = PhotoRules.Instance.GetActivityPhotosCount(m_activityId);
        if (m_Count == 0)
        {
            pn_NoPhotoTip.Visible = true;
            lb_ImageCount.Visible = false;
        }
        else
        {
            lb_ImageCount.Text = "共" + m_Count + "张照片";
            this.Pager.RecordCount = m_Count;
            m_PageSize = this.Pager.PageSize;   
        }
    }

    private void InitdlActivityImage()
    {
        dl_ActivityImage.DataSource = PhotoRules.Instance.GetActivityPhotos(m_activityId, (this.Pager.CurrentPageIndex - 1) * m_PageSize, m_PageSize);
        dl_ActivityImage.DataBind();
        up_ActivityImage.Update();
    }
}
